package com.heima.Server.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.heima.Mapper.ArticleAuthorMapper;
import com.heima.Mapper.ArticleConfigMapper;
import com.heima.Mapper.ArticleContentMapper;
import com.heima.Mapper.ArticleMapper;
import com.heima.Serivce.ArticleCilent;
import com.heima.Server.ArticleMQ;
import com.heima.model.Article.Dto.ArticleHomeDto;
import com.heima.model.Article.Pojo.ApArticle;
import com.heima.model.Article.Pojo.ApArticleConfig;
import com.heima.model.Article.Pojo.ApArticleContent;
import com.heima.model.Article.Pojo.ApAuthor;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmNewstoArticleDto;
import com.heima.model.wemedia.pojos.WmNews;
import com.sun.corba.se.spi.orbutil.threadpool.WorkQueue;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
@Slf4j
public class ArticleService implements com.heima.Server.ArticleService {
    @Autowired
    ArticleFreemaker articleFreemaker;

    @Autowired
    ArticleMapper articleMapper;

    @Autowired
    ArticleContentMapper articleContentMapper;

    @Autowired
    ArticleConfigMapper articleConfigMapper;

    @Autowired
    ArticleAuthorMapper articleAuthorMapper;

    @Autowired
    ArticleMQ articleMQ;

    //加载文章
    @Override
    public ResponseResult ArticleLoad(ArticleHomeDto articleHomeDto) {
        Integer tag=null;
        if(!"__all__".equals(articleHomeDto.getTag()))
        {
            tag=Integer.valueOf(articleHomeDto.getTag());
        }

       List<ApArticle> list= articleMapper.ArticleLoad(articleHomeDto.getSize(),
               tag ,articleHomeDto.getMaxBehotTime(),
                articleHomeDto.getMinBehotTime(),articleHomeDto.getLoaddir());
        return ResponseResult.okResult(list);
    }

    //上拉刷新
    @Override
    public ResponseResult ArticleLoadnew(ArticleHomeDto articleHomeDto) {
        return ArticleLoad(articleHomeDto);
    }

    //加载更多
    @Override
    public ResponseResult ArticleLoadmore(ArticleHomeDto articleHomeDto) {
        return ArticleLoad(articleHomeDto);
    }

//    保存文章到app端
    @Override
    public ResponseResult SaveArticle(WmNewstoArticleDto wmNews) {
//        判断数据是否有效
        if(wmNews==null)
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);

        try{
//        插入文章信息
            ApArticle apArticle=addArticleinfo(wmNews);

            apArticle.setId(GetArticleId(apArticle));
            if(apArticle.getId()==null)
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
//        插入文章内容信息
            ApArticleContent apArticleContent=new ApArticleContent();
            apArticleContent.setArticleId(apArticle.getId());
            apArticleContent.setContent(wmNews.getContent());
            articleContentMapper.insert(apArticleContent);

//        插入文章与文章内容关联信息
            ApArticleConfig apArticleConfig=new ApArticleConfig();
            apArticleConfig.setArticleId(apArticle.getId());
            articleConfigMapper.insert(apArticleConfig);

//            生成页面静态文件
            articleFreemaker.GetStaticArtcle(apArticle.getId());
            log.info("文章上架成功");
//            发送消息给ES
            articleMQ.pushmassgeToES(apArticle.getId());

            return ResponseResult.okResult(apArticle.getId());
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        log.error("文章上架失败");
        return ResponseResult.errorResult(503,"操作失败");
    }

//    文章下架or上架
    @Override
    @Transactional
    public ResponseResult DownOrUpArtiler(Long id,Boolean isDown) {
        if(id!=null)
        {
            QueryWrapper<ApArticleConfig> wrapper=new QueryWrapper<>();
            wrapper.eq("article_id",id);
            ApArticleConfig apArticleConfig=new ApArticleConfig();
            apArticleConfig.setArticleId(id);
            apArticleConfig.setIsDown(isDown);
            articleConfigMapper.update(apArticleConfig,wrapper);
            return ResponseResult.okResult("下架文章成功");
        }
        return ResponseResult.errorResult(500,"操作失败");
    }

    private Long GetArticleId(ApArticle apArticle)
    {
        QueryWrapper<ApArticle> wrapper=new QueryWrapper<>();
        wrapper.eq("author_id",apArticle.getAuthorId()).eq("title",apArticle.getTitle());
        ApArticle one = articleMapper.selectOne(wrapper);
        return one.getId();
    }

//        插入文章信息
    private ApArticle addArticleinfo(WmNewstoArticleDto wmNews)
    {
        QueryWrapper<ApAuthor> wrapper=new QueryWrapper<>();
        wrapper.eq("wm_user_id",wmNews.getUserId());
        ApAuthor author = articleAuthorMapper.selectOne(wrapper);
        ApArticle apArticle=new ApArticle();
        BeanUtils.copyProperties(wmNews,apArticle);
        apArticle.setLayout(wmNews.getType());
        apArticle.setAuthorId((long) author.getId());
        apArticle.setAuthorName(author.getName());
        apArticle.setCreatedTime(new Date());
        apArticle.setPublishTime(new Date());
        articleMapper.insert(apArticle);
        return apArticle;
    }
}
